<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Jenkins流水线语法 | 小阳爱技术</title>
    <meta name="generator" content="VuePress 1.9.5">
    <link rel="icon" href="/img/favicon.ico">
    <link rel="icon" href="/favicon.ico">
    <link rel="manifest" href="/manifest.json">
    <meta name="description" content="Python, Linux, Nginx, Django, Flask, WEB, 前端, Java 全栈知识体系, java体系, java知识体系, java框架,java详解,java学习路线,java spring, java面试, 知识体系, java技术体系, java编程, java编程指南,java开发体系, java开发,java教程,java,java数据结构, 算法, 开发基础">
    <meta name="keywords" content="Python, Linux, Nginx, Django, Flask, WEB, 前端, Java 全栈知识体系, java体系, java知识体系, java框架,java详解,java学习路线,java spring, java面试, 知识体系, java技术体系, java编程, java编程指南,java开发体系, java开发,java教程,java,java数据结构, 算法, 开发基础">
    <meta name="theme-color" content="#11a8cd">
    <meta name="robots" content="all">
    <meta name="author" content="小阳爱技术">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.8cb71cba.css" as="style"><link rel="preload" href="/assets/js/app.9f33dad1.js" as="script"><link rel="preload" href="/assets/js/2.336839c5.js" as="script"><link rel="preload" href="/assets/js/28.2bfb83a2.js" as="script"><link rel="prefetch" href="/assets/js/10.78490e0e.js"><link rel="prefetch" href="/assets/js/11.358c0655.js"><link rel="prefetch" href="/assets/js/12.3d907ab6.js"><link rel="prefetch" href="/assets/js/13.e005782a.js"><link rel="prefetch" href="/assets/js/14.5c1831ed.js"><link rel="prefetch" href="/assets/js/15.3dae17f7.js"><link rel="prefetch" href="/assets/js/16.caf002bb.js"><link rel="prefetch" href="/assets/js/17.96d97c18.js"><link rel="prefetch" href="/assets/js/18.6f49a065.js"><link rel="prefetch" href="/assets/js/19.a72a1b44.js"><link rel="prefetch" href="/assets/js/20.5dbbd998.js"><link rel="prefetch" href="/assets/js/21.3b5c9825.js"><link rel="prefetch" href="/assets/js/22.588d1d79.js"><link rel="prefetch" href="/assets/js/23.bb2ba1d1.js"><link rel="prefetch" href="/assets/js/24.2036f1ef.js"><link rel="prefetch" href="/assets/js/25.7b40ded3.js"><link rel="prefetch" href="/assets/js/26.51a93ad7.js"><link rel="prefetch" href="/assets/js/27.23a56743.js"><link rel="prefetch" href="/assets/js/29.b8c6cf94.js"><link rel="prefetch" href="/assets/js/3.00f81f09.js"><link rel="prefetch" href="/assets/js/30.fe19a5d5.js"><link rel="prefetch" href="/assets/js/31.b68040bb.js"><link rel="prefetch" href="/assets/js/32.d9d3dba8.js"><link rel="prefetch" href="/assets/js/33.59561c54.js"><link rel="prefetch" href="/assets/js/34.243786a2.js"><link rel="prefetch" href="/assets/js/35.daf1b05e.js"><link rel="prefetch" href="/assets/js/36.13258281.js"><link rel="prefetch" href="/assets/js/37.4b591e31.js"><link rel="prefetch" href="/assets/js/38.13d6ce06.js"><link rel="prefetch" href="/assets/js/39.c51dd517.js"><link rel="prefetch" href="/assets/js/4.72262f85.js"><link rel="prefetch" href="/assets/js/40.227cf12d.js"><link rel="prefetch" href="/assets/js/41.8ada8611.js"><link rel="prefetch" href="/assets/js/42.b168f326.js"><link rel="prefetch" href="/assets/js/43.9df968fc.js"><link rel="prefetch" href="/assets/js/44.1546c9b1.js"><link rel="prefetch" href="/assets/js/45.c29bc1da.js"><link rel="prefetch" href="/assets/js/46.2de30989.js"><link rel="prefetch" href="/assets/js/47.34b5ca9a.js"><link rel="prefetch" href="/assets/js/5.56d0fbc7.js"><link rel="prefetch" href="/assets/js/6.b9ff2c2e.js"><link rel="prefetch" href="/assets/js/7.41c828d4.js"><link rel="prefetch" href="/assets/js/8.82412463.js"><link rel="prefetch" href="/assets/js/9.c499cdfa.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8cb71cba.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="小阳爱技术" class="logo"> <span class="site-name can-hide">小阳爱技术</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Python" class="dropdown-title"><a href="/python" class="link-title">Python</a> <span class="title" style="display:none;">Python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c6f51e/404" class="nav-link">暂无</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="DevOPs" class="dropdown-title"><a href="/devops/" class="link-title">DevOPs</a> <span class="title" style="display:none;">DevOPs</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Git</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Git核心命令</a></li></ul></li><li class="dropdown-item"><h4>GitLab</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Gitlab基础</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">CI/CD语法</a></li></ul></li><li class="dropdown-item"><h4>Jernkins</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins入门</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Pipeline流水线语法</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins集成各种工具</a></li></ul></li><li class="dropdown-item"><h4>企业级流水线案例</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">企业级流水线的两个场景</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Npm企业流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Java企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Python企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Golang企业级流水线案例</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/pages/beb6c0bd8a66cea6/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/beb6c0bd8a66cea6/" class="nav-link">网站</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com//" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="/img/logo.png"> <div class="blogger-info"><h3>小阳爱技术</h3> <span>数据库管理员 &amp;&amp; 容器云狂热分子</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Python" class="dropdown-title"><a href="/python" class="link-title">Python</a> <span class="title" style="display:none;">Python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/c6f51e/404" class="nav-link">暂无</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="DevOPs" class="dropdown-title"><a href="/devops/" class="link-title">DevOPs</a> <span class="title" style="display:none;">DevOPs</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Git</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Git核心命令</a></li></ul></li><li class="dropdown-item"><h4>GitLab</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Gitlab基础</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">CI/CD语法</a></li></ul></li><li class="dropdown-item"><h4>Jernkins</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins入门</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Pipeline流水线语法</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Jenkins集成各种工具</a></li></ul></li><li class="dropdown-item"><h4>企业级流水线案例</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">企业级流水线的两个场景</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Npm企业流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Java企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Python企业级流水线案例</a></li><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">Golang企业级流水线案例</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="收藏" class="dropdown-title"><a href="/pages/beb6c0bd8a66cea6/" class="link-title">收藏</a> <span class="title" style="display:none;">收藏</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/beb6c0bd8a66cea6/" class="nav-link">网站</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com//" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>git</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>gitlab</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>jenkins</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/4e044c/" class="sidebar-link">Jenkins入门</a></li><li><a href="/pages/c6f51e/" aria-current="page" class="active sidebar-link">Jenkins流水线语法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/c6f51e/#jenkins-流水线-pipeline-语法" class="sidebar-link">Jenkins 流水线 pipeline 语法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/c6f51e/#pipline-语法" class="sidebar-link">Pipline 语法</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#创建一个流水线项目" class="sidebar-link">创建一个流水线项目</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#pipeline" class="sidebar-link">pipeline</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#agent" class="sidebar-link">agent</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#stages" class="sidebar-link">stages</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#post" class="sidebar-link">post</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#environment" class="sidebar-link">environment</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#parameters" class="sidebar-link">parameters</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#triggers" class="sidebar-link">triggers</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#cron" class="sidebar-link">cron</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#pollscm" class="sidebar-link">pollSCM</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#when" class="sidebar-link">when</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#branch" class="sidebar-link">branch</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#environment-2" class="sidebar-link">environment</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#not" class="sidebar-link">not</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#allof" class="sidebar-link">allOf</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#anyof" class="sidebar-link">anyOf</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#parallel" class="sidebar-link">parallel</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#script" class="sidebar-link">script</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#判断处理" class="sidebar-link">判断处理</a></li><li class="sidebar-sub-header level5"><a href="/pages/c6f51e/#异常处理" class="sidebar-link">异常处理</a></li><li class="sidebar-sub-header level4"><a href="/pages/c6f51e/#流水线语法生成器" class="sidebar-link">流水线语法生成器</a></li></ul></li></ul></li><li><a href="/pages/bc4b74/" class="sidebar-link">Jenkins集成各种工具</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>企业级流水线</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>自动化测试流水线</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=DevOps" title="分类" data-v-06225672>DevOps</a></li><li data-v-06225672><a href="/categories/?category=jenkins" title="分类" data-v-06225672>jenkins</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://blog.abck8s.com" target="_blank" title="作者" class="beLink" data-v-06225672>小阳爱技术</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2022-06-23</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">Jenkins流水线语法<!----></h1>  <div class="theme-vdoing-content content__default"><h2 id="jenkins-流水线-pipeline-语法"><a href="#jenkins-流水线-pipeline-语法" class="header-anchor">#</a> Jenkins 流水线 pipeline 语法</h2> <p>所谓的 Jenkins 流水线 pipeline 语法其实就是 Groovy 语法。</p> <p>每一个 Jenkins 流水线都必须存放在一个名为 jenkinsfile 的文件之中。其中只有一个顶级模块，名称为 pipeline ；在这个顶级模块之下，有诸多小模块。</p> <p>并且，在 Jenkins 中如果要使用 pipeline 流水线的话，必须安装 pipeline 插件。</p> <p><img src="/img/image-20220620204528651.png" alt="image-20220620204528651"></p> <h3 id="pipline-语法"><a href="#pipline-语法" class="header-anchor">#</a> Pipline 语法</h3> <h4 id="创建一个流水线项目"><a href="#创建一个流水线项目" class="header-anchor">#</a> 创建一个流水线项目</h4> <p>pipeline 只有在流水线项目中才能使用，所以在使用 pipeline 之前必须先创建一个流水线项目。</p> <p><img src="/img/image-20220620204749555.png" alt="image-20220620204749555"></p> <h4 id="pipeline"><a href="#pipeline" class="header-anchor">#</a> pipeline</h4> <p>pipeline 是流水线语法的根节点。</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="agent"><a href="#agent" class="header-anchor">#</a> agent</h4> <p>agent 是指定流水线运行在那个环境之中。</p> <ul><li>any：在任何环境中运行</li> <li>none：每一个步骤（stage）都需要设置自己的运行环境。</li> <li>node：指定运行在某个标签的节点之上</li> <li>docker：运行在 docker 之上</li> <li>Dockerfile：从某个 dockerfile 创建的 docker 容器中运行。</li> <li>k8s：运行在某一个 k8s 集群之中</li></ul> <p>案例：</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline<span class="token punctuation">{</span>
    agent none
    stages<span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first stage&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            agent any
            <span class="token function">steps</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first steps&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                echo <span class="token interpolation-string"><span class="token string">&quot;this is first step&quot;</span></span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h4 id="stages"><a href="#stages" class="header-anchor">#</a> stages</h4> <p>stages 主要是用来定义某一个流水线阶段的。其中还包含 stage、steps、script。其中 stage 代表多个阶段；steps 代表的是多个步骤以及 script 代表具体执行的脚本。</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline<span class="token punctuation">{</span>
    agent any
    stages<span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first stage&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">steps</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first steps&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                echo <span class="token interpolation-string"><span class="token string">&quot;Hello World！&quot;</span></span>
                script <span class="token punctuation">{</span>
                    <span class="token keyword">def</span> browsers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'chrome'</span><span class="token punctuation">,</span> <span class="token string">'firefox'</span><span class="token punctuation">]</span>
                    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> browsers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;Testing the </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">browsers<span class="token punctuation">[</span>i<span class="token punctuation">]</span></span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> browser&quot;</span></span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="/img/image-20220620210836823.png" alt="image-20220620210836823"></p> <h4 id="post"><a href="#post" class="header-anchor">#</a> post</h4> <p>根据流水线的最终状态，匹配做一些操作。</p> <p>状态：</p> <ul><li>always：不管运行结果怎么样都运行</li> <li>success：只有当流水线运行结果成功时才运行</li> <li>failure：只有当流水线运行失败时才运行</li> <li>aborted：只有当流水线取消运行时才运行</li> <li>unstable：只有当流水线运行不稳定时才运行</li></ul> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline<span class="token punctuation">{</span>
    agent any
    stages<span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first stage&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">steps</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first steps&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                echo <span class="token interpolation-string"><span class="token string">&quot;Hello World！&quot;</span></span>
                script <span class="token punctuation">{</span>
                    <span class="token keyword">def</span> browsers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'chrome'</span><span class="token punctuation">,</span> <span class="token string">'firefox'</span><span class="token punctuation">]</span>
                    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> browsers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;Testing the </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">browsers<span class="token punctuation">[</span>i<span class="token punctuation">]</span></span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> browser&quot;</span></span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    post <span class="token punctuation">{</span>
        always <span class="token punctuation">{</span>
            echo <span class="token interpolation-string"><span class="token string">&quot;运行成功&quot;</span></span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p><img src="/img/image-20220620211729624.png" alt="image-20220620211729624"></p> <h4 id="environment"><a href="#environment" class="header-anchor">#</a> environment</h4> <p>设置流水线的环境变量。</p> <p><img src="/img/image-20220620212206498.png" alt="image-20220620212206498"></p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline<span class="token punctuation">{</span>
    agent any
    environment <span class="token punctuation">{</span>
        VERSION <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">&quot;1.0.0&quot;</span></span>
    <span class="token punctuation">}</span>
    stages<span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first stage&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">steps</span><span class="token punctuation">(</span><span class="token interpolation-string"><span class="token string">&quot;first steps&quot;</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                echo <span class="token interpolation-string"><span class="token string">&quot;Hello World！&quot;</span></span>
                echo <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">VERSION</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span>
                script <span class="token punctuation">{</span>
                    <span class="token keyword">def</span> browsers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'chrome'</span><span class="token punctuation">,</span> <span class="token string">'firefox'</span><span class="token punctuation">]</span>
                    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> browsers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;Testing the </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">browsers<span class="token punctuation">[</span>i<span class="token punctuation">]</span></span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> browser&quot;</span></span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    post <span class="token punctuation">{</span>
        always <span class="token punctuation">{</span>
            echo <span class="token interpolation-string"><span class="token string">&quot;运行成功&quot;</span></span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h4 id="parameters"><a href="#parameters" class="header-anchor">#</a> parameters</h4> <p>参数化构建，可以设置参数。</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    parameters <span class="token punctuation">{</span> <span class="token function">string</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string">'DEPLOY_ENV'</span><span class="token punctuation">,</span> defaultValue<span class="token punctuation">:</span> <span class="token string">'staging'</span><span class="token punctuation">,</span> description<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token interpolation-string"><span class="token string">&quot;Hello </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">DEPLOY_ENV</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><img src="/img/image-20220621123855344.png" alt="image-20220621123855344"></p> <h4 id="triggers"><a href="#triggers" class="header-anchor">#</a> triggers</h4> <p>triggers 指令定义了 Pipeline 自动化触发的方式。目前只有两个可用的触发器：cron 和 pollSCM。</p> <h5 id="cron"><a href="#cron" class="header-anchor">#</a> cron</h5> <p>接受一个cron风格的字符串来定义Pipeline触发的常规间隔。</p> <h5 id="pollscm"><a href="#pollscm" class="header-anchor">#</a> pollSCM</h5> <p>接受一个cron风格的字符串来定义Jenkins检查SCM源更改的常规间隔。如果存在新的更改，则Pipeline将被重新触发。</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code># 每分钟运行一次
pipeline <span class="token punctuation">{</span>
    agent any
    triggers <span class="token punctuation">{</span>
        <span class="token function">cron</span><span class="token punctuation">(</span><span class="token string">'* * * * *'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Hello World'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h4 id="when"><a href="#when" class="header-anchor">#</a> when</h4> <p>when 指令允许 Pipeline 根据给定的条件确定是否执行该阶段。该 when 指令必须至少包含一个条件。如果 when 指令包含多个条件，则所有子条件必须为 stage 执行返回 true。这与子条件嵌套在一个 allOf 条件中相同（见下面的例子）。</p> <h5 id="branch"><a href="#branch" class="header-anchor">#</a> branch</h5> <p>当正在构建的分支与给出的分支模式匹配时执行，例如：when { branch 'master' }。请注意，这仅适用于多分支 Pipeline。</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Build'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Hello World'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Deploy'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            when <span class="token punctuation">{</span>
                branch <span class="token string">'master'</span>
            <span class="token punctuation">}</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Deploying'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p><img src="/img/image-20220621124841097.png" alt="image-20220621124841097"></p> <h5 id="environment-2"><a href="#environment-2" class="header-anchor">#</a> environment</h5> <p>当指定的环境变量设置为给定值时执行，例如： when { environment name: 'DEPLOY_TO', value: 'production' }</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    environment <span class="token punctuation">{</span>
        DEPLOY_TO <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">&quot;xxx&quot;</span></span>
    <span class="token punctuation">}</span>
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Build'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Hello World'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Deploy'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            when <span class="token punctuation">{</span>
                environment name<span class="token punctuation">:</span> <span class="token string">'DEPLOY_TO'</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'production'</span>
            <span class="token punctuation">}</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Deploying'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p><img src="/img/image-20220621125153950.png" alt="image-20220621125153950"></p> <h5 id="not"><a href="#not" class="header-anchor">#</a> not</h5> <p>当嵌套条件为false时执行。必须包含一个条件。例如：when { not { branch 'master' } }</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Build'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Hello World'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Deploy'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            when <span class="token punctuation">{</span>
                not <span class="token punctuation">{</span>
                    branch <span class="token string">'master'</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Deploying'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h5 id="allof"><a href="#allof" class="header-anchor">#</a> allOf</h5> <p>当所有嵌套条件都为真时执行。必须至少包含一个条件。例如：when { allOf { branch 'master'; environment name: 'DEPLOY_TO', value: 'production' } }</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    environment <span class="token punctuation">{</span>
        DEPLOY_TO <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">&quot;production&quot;</span></span>
    <span class="token punctuation">}</span>
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Build'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Hello World'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Deploy'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            when <span class="token punctuation">{</span>
                allOf <span class="token punctuation">{</span>
                    branch <span class="token string">'master'</span><span class="token punctuation">;</span>
                    environment name<span class="token punctuation">:</span> <span class="token string">'DEPLOY_TO'</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'production'</span> 
                <span class="token punctuation">}</span> 
            <span class="token punctuation">}</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Deploying'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><p><img src="/img/image-20220621131601601.png" alt="image-20220621131601601"></p> <h5 id="anyof"><a href="#anyof" class="header-anchor">#</a> anyOf</h5> <p>当至少一个嵌套条件为真时执行。必须至少包含一个条件。例如：when { anyOf { branch 'master'; branch 'staging' } }</p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    environment <span class="token punctuation">{</span>
        DEPLOY_TO <span class="token operator">=</span> <span class="token interpolation-string"><span class="token string">&quot;production&quot;</span></span>
    <span class="token punctuation">}</span>
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Build'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Hello World'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Example Deploy'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            when <span class="token punctuation">{</span>
                anyOf <span class="token punctuation">{</span>
                    branch <span class="token string">'master'</span><span class="token punctuation">;</span>
                    environment name<span class="token punctuation">:</span> <span class="token string">'DEPLOY_TO'</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> <span class="token string">'production'</span> 
                <span class="token punctuation">}</span> 
            <span class="token punctuation">}</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Deploying'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h4 id="parallel"><a href="#parallel" class="header-anchor">#</a> parallel</h4> <p>通过将阶段设置为parallel来表明该stage为并行运行。</p> <ul><li>一个stage只能有一个steps或者parallel</li> <li>嵌套的stages里不能使用parallel</li> <li>parallel不能包含agent或者tools</li> <li>通过设置failFast 为true表示：并行的job中如果其中的一个失败，则终止其他并行的stage</li></ul> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Non-Parallel Stage'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token string">'Non-parallel'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'Parallel Stage'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            failFast <span class="token boolean">true</span>
            parallel <span class="token punctuation">{</span>
                <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'parallel 1'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    steps <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;parallel 1&quot;</span></span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'parallel 2'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    steps <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;parallel 2&quot;</span></span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h4 id="script"><a href="#script" class="header-anchor">#</a> script</h4> <p>在声明式的pipeline中默认无法使用脚本语法，但是pipeline提供了一个脚本环境入口：script{},通过使用script来包裹脚本语句，即可使用脚本语法。</p> <h5 id="判断处理"><a href="#判断处理" class="header-anchor">#</a> 判断处理</h5> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'stage 1'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                script<span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token interpolation-string"><span class="token string">&quot;1&quot;</span></span> <span class="token operator">==</span> <span class="token interpolation-string"><span class="token string">&quot;1&quot;</span></span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;lalala&quot;</span></span>
                    <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;oooo&quot;</span></span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><img src="/img/image-20220621132453824.png" alt="image-20220621132453824"></p> <h5 id="异常处理"><a href="#异常处理" class="header-anchor">#</a> 异常处理</h5> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'stage 1'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                script<span class="token punctuation">{</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        sh <span class="token string">'exit 1'</span>
                    <span class="token punctuation">}</span>
                    <span class="token keyword">catch</span> <span class="token punctuation">(</span>exc<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        echo <span class="token string">'Something failed'</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p><img src="/img/image-20220621132413842.png" alt="image-20220621132413842"></p> <h4 id="流水线语法生成器"><a href="#流水线语法生成器" class="header-anchor">#</a> 流水线语法生成器</h4> <p>流水线常用的语法就讲到这里。但是流水线每一个插件都会有自己的 pipeline 语句，所以我们无论如何是学不完的，正是因为这一点 Jenkins 为我们提供了一个流水线步骤自动生成器。</p> <p><img src="/img/image-20220621132736400.png" alt="image-20220621132736400"></p> <p>下面我们用 pipeline 写一个动态获取 Git 分支和 tag 的功能。</p> <p><img src="/img/image-20220621133029981.png" alt="image-20220621133029981"></p> <p><img src="/img/image-20220621133707445.png" alt="image-20220621133707445"></p> <p><img src="/img/image-20220621133146082.png" alt="image-20220621133146082"></p> <div class="language-groovy line-numbers-mode"><pre class="language-groovy"><code>pipeline <span class="token punctuation">{</span>
    agent any
    parameters <span class="token punctuation">{</span>
        listGitBranches 
        	branchFilter<span class="token punctuation">:</span> <span class="token string">'refs/heads/(.*)'</span><span class="token punctuation">,</span>
            credentialsId<span class="token punctuation">:</span> <span class="token string">'68a87abf-6af7-4e60-94dd-cf45c280c247'</span><span class="token punctuation">,</span> 
            defaultValue<span class="token punctuation">:</span> <span class="token string">''</span><span class="token punctuation">,</span> 
            listSize<span class="token punctuation">:</span> <span class="token string">'5'</span><span class="token punctuation">,</span> 
            name<span class="token punctuation">:</span> <span class="token string">'GIT_BRANCH'</span><span class="token punctuation">,</span> 
            quickFilterEnabled<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> 
            remoteURL<span class="token punctuation">:</span> <span class="token string">'git@gitlab.abck8s.com:xiaoyangaijishu/html.git'</span><span class="token punctuation">,</span> 
            selectedValue<span class="token punctuation">:</span> <span class="token string">'NONE'</span><span class="token punctuation">,</span> 
            sortMode<span class="token punctuation">:</span> <span class="token string">'NONE'</span><span class="token punctuation">,</span> 
            tagFilter<span class="token punctuation">:</span> <span class="token string">'*'</span><span class="token punctuation">,</span> 
            type<span class="token punctuation">:</span> <span class="token string">'PT_BRANCH'</span>
<span class="token punctuation">}</span>

    stages <span class="token punctuation">{</span>
        <span class="token function">stage</span><span class="token punctuation">(</span><span class="token string">'stage 1'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            steps <span class="token punctuation">{</span>
                echo <span class="token interpolation-string"><span class="token string">&quot;</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token expression">GIT_BRANCH</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">&quot;</span></span>
                script<span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token interpolation-string"><span class="token string">&quot;1&quot;</span></span> <span class="token operator">==</span><span class="token interpolation-string"><span class="token string">&quot;1&quot;</span></span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;lalala&quot;</span></span>
                    <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
                        echo <span class="token interpolation-string"><span class="token string">&quot;oooo&quot;</span></span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    post <span class="token punctuation">{</span>
      always <span class="token punctuation">{</span>
        emailext body<span class="token punctuation">:</span> <span class="token string">'123'</span><span class="token punctuation">,</span> subject<span class="token punctuation">:</span> <span class="token string">'456'</span><span class="token punctuation">,</span> to<span class="token punctuation">:</span> <span class="token string">'chenyangqit@163.com'</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div></div></div>  <div class="page-edit"><div class="edit-link"><a href="https://github.com//edit/master/docs/05.DevOps/03.jenkins/02.Jenkins流水线语法.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2022/06/24, 12:24:14</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/4e044c/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Jenkins入门</div></a> <a href="/pages/bc4b74/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">Jenkins集成各种工具</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/4e044c/" class="prev">Jenkins入门</a></span> <span class="next"><a href="/pages/bc4b74/">Jenkins集成各种工具</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/894626/"><div>
            企业级流水线的两种部署场景
            <!----></div></a> <span class="date">06-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/bc4b74/"><div>
            Jenkins集成各种工具
            <!----></div></a> <span class="date">06-23</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/30f9ff/"><div>
            01、Git
            <!----></div></a> <span class="date">06-22</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:chenyangqit@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://juejin.cn/user/730513894553032/course" title="掘金" target="_blank" class="iconfont icon-juejin"></a><a href="/img/weixin.jpg" title="微信" target="_blank" class="iconfont icon-weixin"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2019-2022
    <span>小阳爱技术 | <a href="https://blog.abck8s.com" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.9f33dad1.js" defer></script><script src="/assets/js/2.336839c5.js" defer></script><script src="/assets/js/28.2bfb83a2.js" defer></script>
  </body>
</html>
